package cn.it.service.impl;

import cn.it.dao.OrderSettingDao;
import cn.it.pojo.OrderSetting;
import cn.it.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {

        //批量导入预约设置数据
        if (null != list && list.size() > 0) {

            for (OrderSetting orderSetting : list) {

                //判断当前日期是否已经进行了预约设置
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //已经进行了预约设置，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //没有进行预约设置，执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    //根据月份查询对应的预约设置数据
    public List<Map> getOrderSettingByMonth(String date) {

        //2019-6-1
        String begin = date + "-1";
        //2019-6-31
        String end = date + "-31";

        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);

        //调用DAO，根据日期范围查询预约设置数据
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);

        List<Map> result = new ArrayList<>();

        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {

                Map<String, Object> m = new HashMap<>();

                m.put("date", orderSetting.getOrderDate().getDate());
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }

        return result;
    }

    @Override
    //根据日期设置对应的预约设置数据
    public void editNumberByDate(OrderSetting orderSetting) {

        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //当前日期已经进行了预约设置，需要执行更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期没有就那些预约设置，需要执行插入操作
            orderSettingDao.add(orderSetting);
        }
    }
}
